/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.thread;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VANDH
 */
public class TinhFibo extends Thread {

    public int n = 0;
    public int result = 0;
    public IntegerWrap count;

    @Override
    public void run() {
        // Dãy số Fibonacci : 1 = ,f1 = 1, f2 = 2,3,5,8...
        // Viết hàm tính phần tử Fn
        int fn = 0;

        if (n <= 2) {
            fn = 1;
            result = fn;
        }

        int f1 = 1, f2 = 1;
        for (int i = 3; i <= n; i++) {
            // tính phần tử thứ i
            fn = f1 + f2;
            f1 = f2;
            f2 = fn;
            try {
                synchronized (count) {
                    count.increment();
                    System.out.println("Fibo - " + fn + ", count = " + count.getInt());
                    count.wait(1000);
                    Thread.sleep(500);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        result = fn; // return fn
    }
}
